package com.org.zp.ctrl.job;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.URLHelper;
import com.org.zp.R;
import com.org.zp.entity.job.Artical;
import com.org.zp.entity.job.Channel;

public class JobActivity extends BaseActivity {
	private TextView title;
	private Button back_btn;
	private Button enable_btn;
	private List<Artical> articals;
	private ProgressDialog progressDialog;
	private LayoutInflater inflater = null;
	private LinearLayout mainLinearLayout = null;
	private int i = 1;
	
	private List<Channel> channels = new ArrayList<Channel>();


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if (!(ActivityTaskManager.getInstance().containsName("JobActivity"))) {
			ActivityTaskManager.getInstance().putActivity("JobActivity", this);
		}

		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mainLinearLayout = (LinearLayout) inflater.inflate(R.layout.job, null);
		setContentView(mainLinearLayout);
		title = (TextView) mainLinearLayout.findViewById(R.id.headTitle);
		back_btn = (Button) mainLinearLayout.findViewById(R.id.back);
		back_btn.setText("  返回");
		back_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityTaskManager.getInstance().removeActivity("JobActivity");
			}
		});
		enable_btn = (Button) mainLinearLayout.findViewById(R.id.top_right_button);
		title.setText("职场咨询");
		enable_btn.setVisibility(View.INVISIBLE);
		
		if (HttpUtils.checkNetwork(JobActivity.this, true)) {
			if(channels.size() == 0){
				progressDialog = ProgressDialog.show(JobActivity.this, "请等待...",
						"正在加载信息，请稍后...", true, true);
				new QuerySearchSelectAsyncTask().execute(URLHelper.mPrefix);
			}
			
		}
	}
	
	class QuerySearchSelectAsyncTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			map.put("act", "articleCategoryList");
			
			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {

			if (result == null || result.equals("")) {
				// 发送解析数据 消息
				Log.e("TMG", "searchPosition result = null");
				progressDialog.dismiss();
			} else if (result.equals("timeout")) {
				// 发送解析数据 消息
				Log.e("TMG", "searchPosition result = timeout");
				progressDialog.dismiss();
			} else {
				parseJson(result);
			}

		}
	}

	private void parseJson(String str) {
		progressDialog.dismiss();
		try {
			JSONObject json = new JSONObject(str);
			String data = json.getString("dataList");
			JSONArray jsonlist = new JSONArray(data);
			for (int i = 0; i < jsonlist.length(); i++) {
				JSONObject log = jsonlist.getJSONObject(i);
				Channel chann = new Channel();
				chann.setId(log.getString("id"));
				chann.setName(log.getString("categoryname"));
				
				String artData = log.getString("articleList");
				JSONArray artlist = new JSONArray(artData);
				ArrayList<Artical> arrayArt = new ArrayList<Artical>();
				for(int j = 0; j < artlist.length(); j++){
					JSONObject log1 = artlist.getJSONObject(j);
					Artical art = new Artical();
					art.setId(log1.getString("id"));
					art.setTitle(log1.getString("title"));
					arrayArt.add(art);
				}
				
				chann.setArticals(arrayArt);
				
				channels.add(chann);
			}
			
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		LinearLayout guideLinearLayout = (LinearLayout) mainLinearLayout
				.findViewById(R.id.contents);
		
		guideLinearLayout.removeAllViews();
		
		for (final Channel channel : channels) {
			LinearLayout guide_typesLayout = (LinearLayout) inflater
					.inflate(R.layout.job_types_title, null);
			if (i == 1) {
				i++;
				LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.FILL_PARENT,
						LinearLayout.LayoutParams.WRAP_CONTENT);
				lp.setMargins(0, 3, 0, 5);
				guide_typesLayout.setLayoutParams(lp);
			} else {
				LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.FILL_PARENT,
						LinearLayout.LayoutParams.WRAP_CONTENT);
				lp.setMargins(0, 15, 0, 5);
				guide_typesLayout.setLayoutParams(lp);

			}
			TextView titleTextView = (TextView) guide_typesLayout
					.findViewById(R.id.tv_guidtype_title);
			titleTextView.setText(channel.getName());
			Button morebutton = (Button) guide_typesLayout
					.findViewById(R.id.more_button);
			morebutton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(JobActivity.this,
							JobClassActivity.class);
					intent.putExtra("id", channel.getId());
					intent.putExtra("title", channel.getName());
					startActivity(intent);
				}
			});
			guideLinearLayout.addView(guide_typesLayout);
			articals = channel.getArticals();
			int y = 0;
			for (final Artical artical : articals) {
				LinearLayout guide_typesitem = (LinearLayout) inflater
						.inflate(R.layout.job_types_items, null);

				if (y == 0) {
					guide_typesitem
							.setBackgroundResource(R.drawable.list_above_background);
				} else if (y == 4) {
					guide_typesitem
							.setBackgroundResource(R.drawable.list_below_background);

				} else {
					guide_typesitem
							.setBackgroundResource(R.drawable.list_mid_background);
				}
				final TextView title = (TextView) guide_typesitem
						.findViewById(R.id.tv_guilditems_title);
				title.setText(artical.getTitle().trim());
				guide_typesitem
						.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								Intent intent = new Intent(JobActivity.this,
										JobContentsActivity.class);
								intent.putExtra("id", artical.getId());
								startActivity(intent);
							}
						});

				guideLinearLayout.addView(guide_typesitem);
				y++;
			}
			guideLinearLayout.addView(new View(JobActivity.this));
		}
	}

	/*@Override
	protected void onResume() {
		super.onResume();
		if(channels.size() == 0){
			progressDialog = ProgressDialog.show(JobActivity.this, "请等待...",
					"正在加载信息，请稍后...", true, true);
			new QuerySearchSelectAsyncTask().execute(URLHelper.mPrefix);
		}
	}
	*/
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity("JobActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
